import { defineStore } from "pinia";
import BomMain from "@/handle/data/complex/BomMain";
import BaseApi from "@/handle/api/base/BaseApi";
import { deepClone } from "@/utils/data/cloneObject";
const baseApi = new BaseApi<BomMain>("/material-category");

export interface LocalEntityPage {
  entityList: BomMain[];
  selectedRow?: BomMain;
  dialogVisible?: boolean;
  dialogCategory?: string;
}

export const useBomMainStore = defineStore({
  id: "app-name",
  state: (): LocalEntityPage => {
    return {
      entityList: [],
      selectedRow: {} as BomMain,
      dialogVisible: false,
      dialogCategory: "",
    };
  },
  getters: {
    getEntityList(): BomMain[] {
      return this.entityList;
    },
    getDialogVisible(): boolean {
      return this.dialogVisible as boolean;
    },
    getEntitySelectedRow(): BomMain {
      return this.selectedRow as BomMain;
    },
  },
  actions: {
    async setEntityList(condition: BomMain) {
      try {
        console.log("cui store test");
        console.log(condition);
        const response = await baseApi.listEntityByCondition(condition);

        console.log(response);
        this.entityList = response;
      } catch (e) {
        return Promise.reject(e);
      }
    },
    openDialog() {
      this.dialogVisible = true;
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    setEntitySelectedRow(row: BomMain) {
      this.selectedRow = deepClone(row);
    },
  },
});
